<template>
    <div class="m-admin-admin-list">
        <div class="row-content am-cf">
            <div class="row">
                <div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
                    <div class="widget am-cf">
                        <div class="widget-head am-cf">
                            <div class="widget-title a m-cf">卡列表</div>
                        </div>
                        <div class="widget-body">
                            <!-- 工具栏 -->
                            <div class="page_toolbar am-margin-bottom-xs">
                                <div class="am-form-group">
                                    <div class="am-btn-group am-btn-group-xs">
                                        <router-link :to="'/admin/cardlist/add'">
                                        <a
                                            class="am-btn am-btn-default am-btn-success"
                                        >
                                            <span class="am-icon-plus"></span> 新增
                                        </a>
                                        </router-link>
                                    </div>
                                </div>
                            </div>
                            <div class="am-scrollable-horizontal">
                                <el-table
                                    stripe
                                    :data="pageOne.list"
                                    row-class-name="m-table-row"
                                    style="width: 100%">
                                    <el-table-column
                                        prop="role_id"
                                        label="角色ID"
                                        width="180">
                                    </el-table-column>
                                    <el-table-column
                                        prop="role_name"
                                        label="角色名称"
                                        width="180">
                                    </el-table-column>
                                    <el-table-column
                                        prop="role_code"
                                        label="角色编码"
                                    >
                                    </el-table-column>
                                    <el-table-column
                                        prop="add_time"
                                        label="添加时间">
                                    </el-table-column>
                                    <el-table-column
                                        fixed="right"
                                        label="操作"
                                        width="200"
                                    >
                                        <template slot-scope="scope">
                                            <router-link :to="{path: '/admin/rolelist/add', query: {role_id: scope.row.role_id}}"><el-button type="primary" size="mini" plain icon="el-icon-edit">编辑</el-button></router-link>
                                            <el-button type="danger" size="mini" @click="delItem(scope.row)" plain icon="el-icon-delete">删除</el-button>
                                        </template>
                                    </el-table-column>
                                </el-table>
                            </div>
                            <div class="block am-margin-bottom-xs am-text-right am-margin-sm">
                                <el-pagination
                                    @size-change="handleSizeChange"
                                    @current-change="handleCurrentChange"
                                    :current-page="pageOne.page.page"
                                    :page-sizes="[20, 50, 100, 400]"
                                    :page-size="pageOne.page.size"
                                    layout="total, sizes, prev, pager, next, jumper"
                                    :total="pageOne.page.total"
                                >
                                </el-pagination>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import { list as roleList, del as roleDel } from '../../api/admin/role.js';

export default {
    name: 'adminList',
    components: {
    },
    data () {
        return {
            pageOne: {
                page: {
                    size: 20,
                    page: 1,
                    total: 0
                },
                list: [],
                form: {
                    name: ''
                }
            }
        }
    },
    mounted () {
        this.getList();
    },
    methods: {
        getList () {
            roleList({page: this.pageOne.page.page, size: this.pageOne.page.size}).then(res => {
                if (res.errno === 0) {
                    this.pageOne.list = res.data.data;
                    this.pageOne.page.page = res.data.currentPage;
                    this.pageOne.page.size = res.data.pageSize;
                    this.pageOne.page.total = res.data.count;
                } else {
                    this.pageOne.list = [];
                    this.pageOne.page.page = 1;
                    this.pageOne.page.total = 0;
                }
            });
        },
        del (items) {
            roleDel(items).then(res => {
                if (res.errno === 0) {
                    this.$message({
                        type: 'success',
                        message: res.errmsg || '删除成功'
                    });
                    this.getList();
                } else {
                    this.$message({
                        type: 'info',
                        message: res.errmsg || '删除失败'
                    });  
                }
            });
        },
        delItem (item) {
            console.log(item);
            this.$confirm('此操作将删除该条记录, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.del([item]);
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });          
            });
        },
        handleSizeChange(val) {
            this.pageOne.page.size = val;
            this.getList();
        },
        handleCurrentChange(val) {
            this.pageOne.page.page = val;
            this.getList();
        }
    }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.m-admin-admin-list{

}
</style>
